/* eslint-disable no-empty */
import axios from 'axios'
import { getToken } from '@/utils/auth'

import store from '@/store'
import { containsAny, containsAll } from '@/utils/array-util'

export function goBacks() {
  this.$router.go(-1)
}
export function Verify(content) {
  if (!content.replace(/(^\s*)|(\s*$)/g, '') === '') {
    return false
  }
  return true
}
export function uploadDocument(formData) {
  axios({
    url: process.env.VUE_APP_BASE_API + '/api/fd/document/upload',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data;charset=utf-8',
      Authorization: getToken()
    }
  })
    .then((res) => {
      if (res.data.code === 1) {
        this.$message.success('上传成功')
        this.fileList = []
        this.$refs.upload.clearFiles()
        this.$parent.getList()
        this.$emit('uploadRes', res)
      }
      this.$message.error('上传失败')
    })
    .catch((err) => {
      console.log(err)
    })
}

export function arrDifferent(data) {
  for (var i = 0; i < data.length; i++) {
    for (var j = i + 1; j < data.length; j++) {
      if (data[i].id === data[j].id) {
        data.splice(j, 1)
        j--
      }
    }
  }
  return data
}

/**
 * @description: 给树结构一个勾选属性
 * @param {Array} data 数组
 * @return {Array} redoData 数组
 */
export function appendIsSelection(data) {
  const newData = Array.from(data)
  newData.forEach(e => {
    e.isSelection = false
  })
  fun_recursion(newData)
}
/**
 * @description: 递归处理
 * @param {Array} data 数组
 * @return {*}
 */

function fun_recursion(data) {
  data.forEach(e => {
    if (e.children) {
      fun_recursion(e.children)
    } else {
      e.isSelection = false
    }
  })
}

import Sortable from 'sortablejs'

// 列拖拽
export function columnDrop(sortable, dropCol) {
  const wrapperTr = document.querySelector('.el-table__header-wrapper tr')
  sortable = Sortable.create(wrapperTr, {
    animation: 180,
    delay: 0,
    onEnd: (evt) => {
      const oldItem = dropCol[evt.oldIndex]
      dropCol.splice(evt.oldIndex, 1)
      dropCol.splice(evt.newIndex, 0, oldItem)
    }
  })
}

/**
 * 判断当前用户是否具有任一个角色
 *
 * @param {*} e 角色
 * @returns 返回true表示具有，否则返回false表示不具有
 */
export function isInRole(e) {
  return containsAny(store.state.user.roles, e)
}

/**
 * 判断当前用户是否具有全部的角色
 *
 * @param {*} e 角色
 * @returns 返回true表示具有，否则返回false表示不具有
 */
export function isInAllRole(e) {
  return containsAll(store.state.user.roles, e)
}

/**
 * @description: 树结构数据展开
 * @param {Array} arr 树结构数组
 * @return {Array} result 一维数组
 */
export function expandArr(arr) {
  let result = []
  arr.forEach((item) => {
    var res = JSON.parse(JSON.stringify(item))
    delete res['children']
    result.push(res)
    if (item.children instanceof Array && item.children.length > 0) {
      result = result.concat(expandArr(item.children))
    }
  })
  return result
}

/**
 * 两数组合并去重
 * @param {Array} aData 数组1
 * @param {Array} bData 数组2
 * @param {string} isTerm 判断相同条件
 * @returns {Array} newArr
 */
export function clearRepetitive(aData, bData, isTerm) {
  let newArr = aData || []
  const list = bData
  newArr.forEach((e) => {
    bData.forEach((res, index) => {
      if (e[isTerm].indexOf(res[isTerm]) !== -1) {
        list.splice(index, 1)
      }
    })
  })
  newArr = [...newArr, ...list]
  return newArr
}

/**
 * 两数组合并去重,返回不同数组
 * @param {Array} aData 数组1
 * @param {Array} bData 数组2
 * @param {string} isTerm 判断相同条件
 * @returns {Array} newArr
 */
export function clearRepetitiveNewArr(aData, bData, isTerm) {
  const newArr = aData.map(item => item.id)
  const result = bData.filter(item => !newArr.includes(item.id))
  const res = { arr: bData, res: result }

  return res
}

/**
 * 处理提交数据
 * @param {Array} Arr 待处理数组
 * @param {string} name 提交的参数属性name
 * @returns {Array} newArr
 */
export function clearPushArr(Arr, name) {
  const newArr = []
  Arr.forEach(e => {
    newArr.push(e[name])
  })
  return newArr
}

export default class UglyTreeNode {
  constructor(val) {
    const self = this

    this.value = val || null
    this.valueListener = null

    this.childArray = []

    this.setValueListener = function(listener) {
      if (listener === null || listener instanceof Function) {
        self.valueListener = listener
      }
      return true
    }

    this.setValue = function(value) {
      self.value = value
      if (self.valueListener) {
        self.valueListener(value)
      }
    }

    this.getValue = function() {
      return self.value
    }

    this.addChild = function(child) {
      if (child instanceof TreeNode) {
        self.childArray.push(child)
        return true
      }
      return false
    }

    this.removeChild = function(child) {
      if (child instanceof TreeNode) {
        self.childArray.splice(self.childArray.indexOf(child), 1)
        return true
      }
      return false
    }

    this.children = function() {
      return self.childArray
    }
  }
}
